package com.i2works.smartluolong.api.controller.index;


import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.entity.ListParamsQuery;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.exception.ServiceException;
import com.i2works.smartluolong.web.entity.items.*;
import com.i2works.smartluolong.web.entity.org.OrgDO;
import com.i2works.smartluolong.web.entity.org.OrgQuery;
import com.i2works.smartluolong.web.service.items.IItemsCategoryService;
import com.i2works.smartluolong.web.service.items.IItemsService;
import com.i2works.smartluolong.web.service.items.IMainItemsService;
import com.i2works.smartluolong.web.service.org.IOrgService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 主页控制器API
 *
 * @author koal <koal@vip.qq.com>
 * @date 2017/9/27 20:07
 */

@RestController
@RequestMapping
public class IndexController extends BaseModule {

    private final IItemsCategoryService itemsCategoryService;

    private final IMainItemsService mainItemsService;

    private final IItemsService itemsService;
    @Autowired
    private IOrgService orgService;

    public IndexController(IItemsCategoryService itemsCategoryService, IItemsService itemsService, IMainItemsService mainItemsService) {
        this.itemsCategoryService = itemsCategoryService;
        this.mainItemsService = mainItemsService;
        this.itemsService = itemsService;
    }

    /**
     * 主页
     */
    @GetMapping("/")
    public Result index() {
        return R.success();
    }

    /**
     * 主页
     */
    @PostMapping("/cateList")
    public Result cateList() {
        JSONObject json = new JSONObject();
        /***
         * 事项分类
         * */
        ItemsCategoryQuery query = new ItemsCategoryQuery();
        query.setStatus(1);
        List<ItemsCategoryDO> catelist = new ArrayList<ItemsCategoryDO>();
        try {
            catelist = itemsCategoryService.queryList(query);
				for (int i = 0; i < catelist.size(); i++) {
					if(catelist.get(i).getId()==27) {
						catelist.remove(i);
					}
				}
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        json.put("catelist", catelist);
        return R.success(json);
    }


    @PostMapping(value = {"/mainList/{cateId}"})
    public Result mainlist(@ModelAttribute ListParamsQuery params) {
        JSONObject json = new JSONObject();
        try {
            //主事项
            MainItemsQuery mainQuery = new MainItemsQuery();
            mainQuery.setCateId(params.getCateId());
            mainQuery.setStatus(1);
            List<MainItemsDO> mainList = mainItemsService.queryList(mainQuery);
            json.put("mainList", mainList);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return R.success(json);
    }

    /**
     *
     * 部门列表
     */
    @PostMapping("/deptList")
    @ResponseBody
    public Result list(OrgQuery query) {
        try {
            List<OrgDO> list= Lists.newArrayList();
            query.setStatus(1);
            query.setLimit(500L);
            Integer count=	orgService.queryCount(query);
            if(count>0) {
                list=	orgService.queryList(query);
            }
            return R.success(list,count);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.error();
        }

    }

    @PostMapping(value = {"/itemList/{cateId}","/itemList/{cateId}/{mainId}"})
    public Result itemList(@ModelAttribute ListParamsQuery params) {
        JSONObject json = new JSONObject();
        try {
            //子事项
            ItemsQuery query = new ItemsQuery();
            query.setCateId(params.getCateId());
            query.setMainId(params.getMainId());
            query.setStatus(1);
            List<ItemsDO> itemList = itemsService.queryList(query);
            json.put("itemList", itemList);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return R.success(json);
    }


}
